package cn.yang.domain.award.service.distribute.impl;

import cn.yang.domain.award.model.aggregate.GiveOutPricesAggregate;
import cn.yang.domain.award.model.entity.DistributeAwardEntity;
import cn.yang.domain.award.model.entity.UserAwardRecordEntity;
import cn.yang.domain.award.model.entity.UserCreditAwardEntity;
import cn.yang.domain.award.model.valobj.AwardStateVO;
import cn.yang.domain.award.repository.IAwardRepository;
import cn.yang.domain.award.service.distribute.IDistributeAward;
import cn.yang.types.common.Constants;
import org.apache.commons.lang3.StringUtils;
import org.apache.tomcat.util.bcel.classfile.Constant;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.MathContext;

/**
 * @version v1.0
 * @ClassName: cn.yang.domain.award.service.distribute.impl
 * @Description: 用户积分奖品，支持award_config透传，满足黑名单积分奖励
 * @Author: YJ
 */
@Component("user_credit_random")
public class UserCreditRandomAward implements IDistributeAward {
    @Resource
    private IAwardRepository awardRepository;

    @Override
    public void giveOutPrices(DistributeAwardEntity distributeAwardEntity) {
        Integer awardId = distributeAwardEntity.getAwardId();
        String awardConfig = distributeAwardEntity.getAwardConfig();
        if (StringUtils.isNotBlank(awardConfig)) {
            // 0.01 - 1 1-100
            awardConfig = awardRepository.queryAwardConfig(awardId);
        }
        String[] creditRange = awardConfig.split(Constants.SPLIT);
        if (creditRange.length != 2) {
            throw new IllegalArgumentException("award_config: " + awardConfig + " 配置不是一个范围值，如1,100");
        }
        // 生成随机积分
        BigDecimal creditAmount = generateRandom(new BigDecimal(creditRange[0]), new BigDecimal(creditRange[1]));

        // 构建聚合对象
        UserAwardRecordEntity userAwardRecordEntity = GiveOutPricesAggregate.buildDistributeAwardRecordEntity(
                distributeAwardEntity.getUserId(),
                distributeAwardEntity.getOrderId(),
                distributeAwardEntity.getAwardId(),
                AwardStateVO.create
        );

        UserCreditAwardEntity userCreditAwardEntity = GiveOutPricesAggregate.buildUserCreditAwardEntity(
                distributeAwardEntity.getUserId(),
                creditAmount);

        GiveOutPricesAggregate giveOutPricesAggregate = GiveOutPricesAggregate.builder()
                .userId(distributeAwardEntity.getUserId())
                .userAwardRecordEntity(userAwardRecordEntity)
                .userCreditAwardEntity(userCreditAwardEntity)
                .build();

        //存储发奖对象
        awardRepository.saveGiveOutPricesAggregate(giveOutPricesAggregate);

    }

    private BigDecimal generateRandom(BigDecimal min, BigDecimal max) {
        if (min.equals(max)) return min;
        BigDecimal randomBigDecimal = min.add(BigDecimal.valueOf(Math.random()).multiply(max.subtract(min)));
        return randomBigDecimal.round(new MathContext(3));
    }
}
